﻿<template>
  <view class="tab-recommend">
    <view class="content-container">
      <view v-if="loading" class="loading-container">
        <text class="loading-text">加载中...</text>
      </view>
      
      <view v-else-if="error" class="error-container">
        <text class="error-text">{{ error }}</text>
        <view class="retry-btn" @tap="loadData">
          <text>重试</text>
        </view>
      </view>
      
      <view v-else-if="dataList.length === 0" class="empty-container">
        <text class="empty-text">暂无推荐内容</text>
      </view>
      
      <view v-else>
        <view 
          class="recommend-item" 
          v-for="(item, index) in dataList" 
          :key="index"
          @tap="onItemClick(item)"
        >
          <view class="item-image">
            <image 
              v-if="item.image" 
              :src="item.image" 
              mode="aspectFill"
              class="real-image"
            />
            <text v-else class="placeholder-emoji">🏞️</text>
          </view>
          <view class="item-content">
            <text class="item-title">{{ item.title || '推荐内容' }}</text>
            <text class="item-desc">{{ item.description || item.content || '精彩内容推荐' }}</text>
            <view class="item-footer">
              <text class="price" v-if="item.price">¥{{ item.price }}</text>
              <text class="price" v-else>免费</text>
              <view class="book-btn">
                <text>查看详情</text>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import sheep from '@/sheep';

const loading = ref(false);
const error = ref('');
const dataList = ref([]);

const loadData = async () => {
  try {
    loading.value = true;
    error.value = '';
    
    const city = await sheep.$helper.getCurrentCity();
    const res = await sheep.$api.noAuth.getHomeContentByType('2', city);

    
    if (res && res.code === 200 && res.data && Array.isArray(res.data)) {
      dataList.value = res.data
        .filter(item => item.status === 1)
        .map(item => ({
          id: item.id,
          title: item.title,
          description: item.synopsis,
          content: item.content,
          image: getFirstValidImage(item.pics),
          url: item.url || null,
          price: item.price || null
        }));
    } else if (res && res.content && Array.isArray(res.content)) {
      dataList.value = res.content
        .filter(item => item.status === 1)
        .map(item => ({
          id: item.id,
          title: item.title,
          description: item.synopsis,
          content: item.content,
          image: getFirstValidImage(item.pics),
          url: item.url || null,
          price: item.price || null
        }));
    } else {
      dataList.value = [];
    }
  } catch (err) {
    error.value = '加载失败，请稍后重试';
    dataList.value = [];
  } finally {
    loading.value = false;
  }
};

const getFirstValidImage = (pics) => {
  if (!pics || pics === 'null' || pics === '') return null;
  
  const picArray = pics.split(',').filter(pic => pic && pic !== 'null' && pic.trim() !== '');
  if (picArray.length === 0) return null;
  
  const imagePath = picArray[0].trim();
  if (imagePath.startsWith('http://') || imagePath.startsWith('https://')) {
    return imagePath;
  }
  
  const baseUrl = sheep.$platform.baseUrl || '';
  return baseUrl + (imagePath.startsWith('/') ? imagePath : '/' + imagePath);
};

const onItemClick = (item) => {
  if (item.url) {
    sheep.$router.go(item.url);
  } else if (item.id) {
    uni.navigateTo({
      url: `/pages/recommend-detail?id=${item.id}`
    });
  }
};

onMounted(() => {
  loadData();
});

defineExpose({
  loadData
});
</script>

<style lang="scss" scoped>
$primary-color: #8B4513;
$secondary-color: #DAA520;
$text-dark: #333;
$text-light: #666;

.tab-recommend {
  background: #fff;
  padding: 20rpx;

  .loading-container,
  .error-container,
  .empty-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 400rpx;
    
    .loading-text,
    .error-text,
    .empty-text {
      font-size: 28rpx;
      color: $text-light;
      margin-bottom: 20rpx;
    }
    
    .retry-btn {
      padding: 16rpx 32rpx;
      background: $primary-color;
      color: white;
      border-radius: 24rpx;
      font-size: 26rpx;
    }
  }

  .recommend-item {
    background: white;
    border-radius: 16rpx;
    padding: 30rpx;
    margin-bottom: 20rpx;
    box-shadow: 0 4rpx 12rpx rgba(0,0,0,0.05);
    display: flex;
    
    .item-image {
      width: 120rpx;
      height: 120rpx;
      background: #f0f0f0;
      border-radius: 12rpx;
      display: flex;
      align-items: center;
      justify-content: center;
      margin-right: 20rpx;
      overflow: hidden;
      
      .real-image {
        width: 100%;
        height: 100%;
      }
      
      .placeholder-emoji {
        font-size: 40rpx;
      }
    }
    
    .item-content {
      flex: 1;
      
      .item-title {
        display: block;
        font-size: 32rpx;
        font-weight: bold;
        color: $text-dark;
        margin-bottom: 10rpx;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      
      .item-desc {
        display: block;
        font-size: 26rpx;
        color: $text-light;
        margin-bottom: 20rpx;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        white-space: normal;
      }
      
      .item-footer {
        display: flex;
        align-items: center;
        justify-content: space-between;
        
        .price {
          font-size: 30rpx;
          font-weight: bold;
          color: $primary-color;
        }
        
        .book-btn {
          padding: 12rpx 24rpx;
          background: linear-gradient(135deg, $primary-color 0%, $secondary-color 100%);
          color: white;
          border-radius: 20rpx;
          font-size: 24rpx;
        }
      }
    }
  }
}
</style> 
